package controllers

import (
	"TTSX_beego/models"
	"TTSX_beego/torch"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/gomodule/redigo/redis"
	"math"
	"strconv"
)

type GoodsController struct {
	beego.Controller
}

func LayoutGoods(This *GoodsController) []models.GoodsType {
	userName := This.GetSession("userName")
	if userName == nil {
		This.Data["userName"] = ""
	} else {
		This.Data["userName"] = userName.(string)
	}

	o := orm.NewOrm()
	var GoodsTypes []models.GoodsType
	o.QueryTable("GoodsType").All(&GoodsTypes)

	This.Data["types"] = GoodsTypes

	return GoodsTypes

}

func (This *GoodsController) ShowIndex() {

	GoodsTypes := LayoutGoods(This)
	o := orm.NewOrm()
	var PowerPoint []models.IndexGoodsBanner
	o.QueryTable("IndexGoodsBanner").OrderBy("Index").All(&PowerPoint)

	var Promotion []models.IndexPromotionBanner
	o.QueryTable("IndexPromotionBanner").OrderBy("Index").All(&Promotion)

	goods := make([]map[string]interface{}, len(GoodsTypes))

	for index, value := range GoodsTypes {
		temp := make(map[string]interface{})

		temp["type"] = value
		goods[index] = temp

	}

	for _, value := range goods {
		var textGoods []models.IndexTypeGoodsBanner
		var imgGoods []models.IndexTypeGoodsBanner

		o.QueryTable("IndexTypeGoodsBanner").RelatedSel("GoodsType", "GoodsSKU").
			OrderBy("Index").Filter("DisplayType", 0).Filter("GoodsType", value["type"]).All(&textGoods)

		o.QueryTable("IndexTypeGoodsBanner").RelatedSel("GoodsType", "GoodsSKU").
			OrderBy("Index").Filter("DisplayType", 1).Filter("GoodsType", value["type"]).All(&imgGoods)

		value["textGoods"] = textGoods
		value["imgGoods"] = imgGoods

	}

	This.Layout = "goodsLayout.html"

	This.Data["goods"] = goods

	This.Data["power"] = PowerPoint
	This.Data["promotion"] = Promotion

	This.TplName = "index.html"
}

func (This *GoodsController) ShowDetail() {

	LayoutGoods(This)
	id, err := This.GetInt("id")
	if err != nil {
		This.Ctx.WriteString("服务器错误")
		return
	}

	o := orm.NewOrm()
	var goodsSKU models.GoodsSKU
	o.QueryTable("GoodsSKU").RelatedSel("GoodsType").Filter("Id", id).One(&goodsSKU)

	var goodsNew []models.GoodsSKU
	o.QueryTable("GoodsSKU").RelatedSel("GoodsType").
		Filter("GoodsType", goodsSKU.GoodsType).OrderBy("Time").Limit(2, 0).All(&goodsNew)

	userName := This.GetSession("userName")
	if userName != nil {
		o := orm.NewOrm()
		var user models.User
		user.Name = userName.(string)
		o.Read(&user, "Name")

		conn, err := redis.Dial("tcp", "192.168.88.137:6379")

		if err != nil {
			beego.Info("err")
		} else {
			defer conn.Close()
			conn.Do("lrem", "history_"+strconv.Itoa(user.Id), 0, id) //删除以前的
			conn.Do("lpush", fmt.Sprintf("history_%d", user.Id), id)
		}

	}

	This.Data["goods"] = goodsSKU
	This.Layout = "goodsLayout.html"
	This.Data["goodsNew"] = goodsNew
	This.TplName = "detail.html"

}

func (This *GoodsController) ShowList() {

	LayoutGoods(This)
	type_id, err := This.GetInt("type_id")
	if err != nil {
		This.Redirect("/", 302)
		return
	}

	var goodsNew []models.GoodsSKU

	o := orm.NewOrm()
	o.QueryTable("GoodsSKU").RelatedSel("GoodsType").Filter("GoodsType__Id", type_id).
		OrderBy("Time").Limit(2, 0).All(&goodsNew)

	//分页
	pageSize := 1
	count, _ := o.QueryTable("GoodsSKU").Filter("GoodsType__Id", type_id).Count()
	pageCount := int(math.Ceil(float64(count) / float64(pageSize)))
	pageIndex, err := This.GetInt("page")
	if err != nil {
		pageIndex = 1
	}

	pages := torch.PageTool(pageIndex, pageCount)

	var goods []models.GoodsSKU

	//排序
	sort := This.GetString("sort")
	if sort == "price" {
		o.QueryTable("GoodsSKU").RelatedSel("GoodsType").
			Filter("GoodsType__Id", type_id).OrderBy("-Price").Limit(pageSize, pageSize*(pageIndex-1)).All(&goods)

	} else if sort == "sale" {
		o.QueryTable("GoodsSKU").RelatedSel("GoodsType").
			Filter("GoodsType__Id", type_id).OrderBy("Sales").Limit(pageSize, pageSize*(pageIndex-1)).All(&goods)
	} else {
		o.QueryTable("GoodsSKU").RelatedSel("GoodsType").
			Filter("GoodsType__Id", type_id).Limit(pageSize, pageSize*(pageIndex-1)).All(&goods)
	}

	This.Data["pages"] = pages
	This.Data["pageIndex"] = pageIndex
	This.Data["pageCount"] = pageCount
	This.Data["sort"] = sort

	This.Data["goodsNew"] = goodsNew
	This.Data["type_id"] = type_id
	This.Data["goods"] = goods
	This.Layout = "goodsLayout.html"
	This.TplName = "list.html"
}

func (This *GoodsController) HandleSearch() {

	LayoutGoods(This)

	search := This.GetString("search")
	o := orm.NewOrm()
	var goods []models.GoodsSKU

	if search == "" {
		o.QueryTable("GoodsSKU").All(&goods)
		This.Data["goods"] = goods
		This.TplName = "search.html"
		This.Layout = "goodsLayout.html"
		return
	}

	o.QueryTable("GoodsSKU").Filter("Name__icontains", search).All(&goods)

	This.Data["goods"] = goods
	This.Data["search"] = search
	This.TplName = "search.html"
	This.Layout = "goodsLayout.html"

}
